/**
 * Created by zhanglihe on 15/11/17.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
], function ($, common, ready, template, Collection, Model, dialog) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function (_args) {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'ios_patch',
                condition: {
                    limit: 10,
                    offset: 0
                },
                event: {},
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            /**
             * 添加事件
             */
            $('#btn-add').on('click', function (e) {
                data = {};
                e.preventDefault();
                _this.dialog=dialog({
                    title: '上传补丁',
                    width: 500,
                    content: template('add-tpl'),
                    ok: function () {
                        _this.data.key = $('#key').val();
                        _this.data.version = $('#version').val();
                        _this.data.url = $('#url').html();
                        _this.data.name = $('#name').html();
                        _this.data.patch_id = $('#patch_id').val();
                        if(!_this.data.key||!_this.data.version||!_this.data.url||!_this.data.name||!_this.data.patch_id){
                            Alert({msg: '请补充完整内容', type: 'error'});
                            return false;
                        }

                        if (!new Model({
                                url: _global.url.api + 'ios_patch',
                                data: _this.data,

                            }).post()) {
                            return false;
                        }
                        _this.collection.reload();
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
            });

            $('body').on('click', '#patch', function (e) {
                e.preventDefault();
                _this.upload_dialog = dialog({
                    content: template('upload-patch-tpl', {}),
                    width: 550,
                    ok: function () {
                        var patch = $("#upload-frame")[0].contentWindow.patch.get();
                        _this.patchPreview(patch);
                        console.log(patch);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $("body").on("click", '.patch-remove', function (e) {
                e.preventDefault();
                $(this).parent('li').remove();
            });

        },
        patchPreview: function (patch) {
            var _this = this;
            console.log(patch.id);
            if (typeof patch.id !== 'undefined') {
                $('#patch-view').html(template('patch-tpls', patch));
            }
            this.patch = patch;
        },
    };

    new index();

});